<script lang="ts" setup>
  import type { InfoGroupApi } from '#/api/spa/group';

  import { computed, ref, h, onMounted,watch,nextTick } from 'vue';
  import { $t } from '#/locales';

 import { Plus } from "@vben/icons";
import { Button, Tabs, Checkbox, Input, Textarea, Select,RadioGroup,CheckboxGroup, DatePicker } from 'ant-design-vue';
import { ImageUpload, FileUpload } from "#/components/upload";
import type { OnActionClickParams } from '#/adapter/vxe-table';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { useInfoStoreGridEditColumns } from '../data';
import { getInfoStoreListByGroupId } from '#/api/spa/group';

const props = defineProps<{
   groupId?: number // 所属集团ID（主表的关联字段）
}>()

 /** 表格操作按钮的回调函数 */
function onActionClick({
 code,
 row,
}: OnActionClickParams<InfoGroupApi.InfoStore>) {
  switch (code) {
    case 'delete': {
      onDelete(row);
      break;
    }
  }
}

const [Grid, gridApi] = useVbenVxeGrid({
gridOptions: {
  columns: useInfoStoreGridEditColumns(onActionClick),
  border: true,
  showOverflow: true,
  autoResize: true,
  keepSource: true,
  rowConfig: {
    keyField: 'id',
  },
  pagerConfig: {
    enabled: false,
  },
  toolbarConfig: {
    enabled: false,
  },
},
});

/** 添加门店 */
const onAdd = async () => {
  await gridApi.grid.insertAt({} as InfoGroupApi.InfoStore, -1);
}

/** 删除门店 */
const onDelete =  async (row: InfoGroupApi.InfoStore) => {
  await gridApi.grid.remove(row);
}

/** 提供获取表格数据的方法供父组件调用 */
defineExpose({
  getData: (): InfoGroupApi.InfoStore[] => {
    const data = gridApi.grid.getData() as InfoGroupApi.InfoStore[];
    const removeRecords = gridApi.grid.getRemoveRecords() as InfoGroupApi.InfoStore[];
    const insertRecords = gridApi.grid.getInsertRecords() as InfoGroupApi.InfoStore[];
    return data
        .filter((row) => !removeRecords.some((removed) => removed.id === row.id))
        .concat(insertRecords.map((row: any) => ({ ...row, id: undefined })));
  },
});

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
    () => props.groupId,
    async (val) => {
      if (!val) {
        return;
      }
      await nextTick();
      await gridApi.grid.loadData(await getInfoStoreListByGroupId(props.groupId!));
    },
    { immediate: true },
);
</script>

<template>
   <Grid class="mx-4">
                 <template #storeCode="{ row }">
                  <Input v-model:value="row.storeCode" />
                </template>
                <template #storeName="{ row }">
                  <Input v-model:value="row.storeName" />
                </template>
                <template #parentStoreId="{ row }">
                  <Input v-model:value="row.parentStoreId" />
                </template>
                <template #storeType="{ row, column }">
                  <Select v-model:value="row.storeType" class="w-full">
                    <Select.Option v-for="option in column.params.options" :key="option.value" :value="option.value">
                      {{ option.label }}
                    </Select.Option>
                  </Select>
                </template>
                <template #contactPerson="{ row }">
                  <Input v-model:value="row.contactPerson" />
                </template>
                <template #contactPhone="{ row }">
                  <Input v-model:value="row.contactPhone" />
                </template>
                <template #contactEmail="{ row }">
                  <Input v-model:value="row.contactEmail" />
                </template>
                <template #address="{ row }">
                  <Input v-model:value="row.address" />
                </template>
                <template #longitude="{ row }">
                  <Input v-model:value="row.longitude" />
                </template>
                <template #latitude="{ row }">
                  <Input v-model:value="row.latitude" />
                </template>
                <template #businessHours="{ row }">
                  <Input v-model:value="row.businessHours" />
                </template>
                <template #status="{ row, column }">
                  <RadioGroup v-model:value="row.status" :options="column.params.options" />
                </template>
                <template #openTime="{ row }">
                  <DatePicker
                      v-model:value="row.openTime"
                      :showTime="true"
                      format="YYYY-MM-DD HH:mm:ss"
                      valueFormat='x'
                  />
                </template>
                <template #config="{ row }">
                  <Input v-model:value="row.config" />
                </template>
                <template #iotConfig="{ row }">
                  <Input v-model:value="row.iotConfig" />
                </template>
                <template #orgId="{ row }">
                  <Input v-model:value="row.orgId" />
                </template>
                <template #businessLicense="{ row }">
                  <Input v-model:value="row.businessLicense" />
                </template>
                <template #storeManager="{ row }">
                  <Input v-model:value="row.storeManager" />
                </template>
                <template #city="{ row }">
                  <Input v-model:value="row.city" />
                </template>
                <template #district="{ row }">
                  <Input v-model:value="row.district" />
                </template>
                <template #handCardCount="{ row }">
                  <Input v-model:value="row.handCardCount" />
                </template>
                <template #roomCount="{ row }">
                  <Input v-model:value="row.roomCount" />
                </template>
                <template #technicianCount="{ row }">
                  <Input v-model:value="row.technicianCount" />
                </template>
  </Grid>
  <div class="flex justify-center -mt-4">
    <Button :icon="h(Plus)" type="primary" ghost @click="onAdd" v-access:code="['spa:info-group:create']">
      {{ $t('ui.actionTitle.create', ['门店']) }}
    </Button>
  </div>
</template>